package com.kuaimacode.kframework.agent.controller.user;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.user.UserCertifacateService;
import com.kuaimacode.kframework.agent.services.user.UserService;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.component.ComponentUtil;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.user.User;
import com.kuaimacode.kframework.mybatis.models.user.UserCertifacate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @author Administrator
 * @date 2019/03/18
 */

/**
 * Controller
 */
@Controller
@RequestMapping({"/user"})
public class UserController {
    private static String PAGE_FILE_NAME = "user/";

    @Autowired
    private UserService userServices;
    @Autowired
    private ComponentUtil componentUtil;
    @Autowired
    private UserCertifacateService certifacateService;

    @RequestMapping(value = {"/index"})
    public String index(@RequestParam(value = "pageNum", defaultValue = PageConstants.PAGE_NUM + "") int pageNum,
                        @RequestParam(value = "pageSize", defaultValue = PageConstants.PAGE_SIZE + "") int pageSize,
                        ModelMap model, User searchUser) {
        PageInfo<User> pageInfo = userServices.findAll(pageNum, pageSize, searchUser);
        model.put("pageInfo", pageInfo);
        model.put("searchUser", searchUser);
        return PAGE_FILE_NAME + "index";
    }

    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(@PathVariable String id, ModelMap map) {
        User user = userServices.getById(id);
        Optional.ofNullable(user.getAvatarUrl())
                .ifPresent(url -> user.setAvatarUrl(componentUtil.convertImgUrl(url)));
        map.put("user", user);
        return PAGE_FILE_NAME + "detail";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable String id, ModelMap map) {
        User user = userServices.getById(id);
        map.put("user", user);
        map.put("isAdd", false);
        return PAGE_FILE_NAME + "form";
    }

    @RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(User user, Boolean isAdd) {
        try {
            if (isAdd) {
                // 新建保存
                userServices.save(user);
            } else {
                // 编辑保存
                userServices.updateById(user);
            }
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable String id) {
        try {
            userServices.removeById(id);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    @RequestMapping(value = "/certifacate/{id}", method = RequestMethod.GET)
    public String certifacate(@PathVariable Long id, ModelMap map) {
        UserCertifacate userCertifacate = Optional.ofNullable(certifacateService.findByUserId(id)).orElse(new UserCertifacate());
        userCertifacate.setHeadPic(componentUtil.convertImgUrl(userCertifacate.getHeadPic()));
        userCertifacate.setCardNegative(componentUtil.convertImgUrl(userCertifacate.getCardNegative()));
        userCertifacate.setCardPositive(componentUtil.convertImgUrl(userCertifacate.getCardPositive()));
        map.put("userCertifacate", userCertifacate);
        return PAGE_FILE_NAME + "certifacate/detail";
    }
}
